<?php
function advancedRmdir($path) {
    $origipath = $path;
    $handler = opendir($path);
    while (true) {
        $item = readdir($handler);
        if ($item == "." or $item == "..") {
            continue;
        } elseif (gettype($item) == "boolean") {
            closedir($handler);
            if (!@rmdir($path)) {
                return false;
            }
            if ($path == $origipath) {
                break;
            }
            $path = substr($path, 0, strrpos($path, "/"));
            $handler = opendir($path);
        } elseif (is_dir($path."/".$item)) {
            closedir($handler);
            $path = $path."/".$item;
            $handler = opendir($path);
        } else {
            unlink($path."/".$item);
        }
    }
    return true;
}

// updating css file
if ($_GET['x'] == $this->options['style'] && $_GET['y'] == 'edit' && !empty($_GET['file']) && file_exists(TEMPLATEPATH.'/styles/'. $_GET['x'] .'/'. $_GET['file']) && is_writable(TEMPLATEPATH.'/styles/'. $_GET['x'] .'/'. $_GET['file']) && !empty($_POST['cp_save_css'])) {
if ($file = fopen(TEMPLATEPATH.'/styles/'.$_GET['x'].'/'.$_GET['file'], 'w')) {
$updated_line = stripslashes($_POST["css_c"]);
fwrite($file, "$updated_line");
$seterror = false;
fclose($file);
} else {
$seterror = true;
}
}
// updating css file

// delete style
if ($_GET['x'] == $this->options['style'] && $_GET['y'] == 'delete' && !empty($_POST['cp_delete_style'])) {
$dirPath = TEMPLATEPATH. '/styles/';
if ($handle = opendir($dirPath)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir("$dirPath/$file")) {
$getstyles[] = $file;
}
}
}
closedir($handle);
}
if (!is_dir($dirPath.$this->options['style']) || $_GET['x'] == $this->default_settings['style']) {
$seterror = true;
} else {
advancedRmdir($dirPath.$this->options['style'].'/');
$seterror = false;
}
}
// delete style

if (!empty($_POST['cp_save'])) {
	$this->options["style"] = $_POST['cp_style'];
	update_option($cpanel['name'], $this->options);
	echo '<div class="updated fade msg" id="message"><p>Your changes have been saved.</p></div>';
}

if (!empty($_POST['cp_save_css'])) {
	if ($seterror == true) {
	echo '<div class="error" id="message"><p>Unable to edit this file. Please check file permissions.</p></div>';
	} elseif ($seterror == false) {
	echo '<div class="updated fade msg" id="message"><p>Your file has been edited successfully.</p></div>';
	}
}

if (!empty($_POST['cp_delete_style'])) {
	if ($seterror == true) {
	echo '<div class="error" id="message"><p>We were unable to delete this style. Are you trying to delete default or non-existent style?</p></div>';
	} elseif ($seterror == false) {
	echo '<div class="updated fade msg" id="message"><p>This style has been successfully deleted.</p></div>';
	}
}
?>

<div class="cpage">
<h2>Manage Styles</h2>

<form action="" method="post">

<fieldset><legend>Theme Style</legend>
<ul>
	<li><label for="cp_style">Choose from available styles</label></li>
	<li>
	<select name="cp_style" class="widefat mid">
	<?php
	$dirPath = TEMPLATEPATH. '/styles/';
	if ($handle = opendir($dirPath)) {
	while (false !== ($file = readdir($handle))) {
	if ($file != "." && $file != "..") {
	if (is_dir("$dirPath/$file")) {
	?>
	<option value="<?php echo $file; ?>"<?php selected($file, $this->options["style"]); ?>><?php echo ucfirst($file); ?></option>
	<?php
	}
	}
	}
	closedir($handle);
	}
	?>
	</select>
	</li>
</ul>
</fieldset>

<input type="submit" value="Update Settings" name="cp_save" class="button-primary" />
</form>

<fieldset><legend>Manage (<?php echo ucfirst($this->options['style']); ?>) style</legend>
<ol>
	<li><a href="?page=<?php echo $_GET['page']; ?>&amp;x=<?php echo $this->options['style']; ?>&amp;y=edit">Edit Style</a></li>
	<li><a href="?page=<?php echo $_GET['page']; ?>&amp;x=<?php echo $this->options['style']; ?>&amp;y=delete">Delete Style</a></li>
</ol>
</fieldset>

<?php if ($_GET['x'] == $this->options['style'] && $_GET['y'] == 'edit' && is_dir(TEMPLATEPATH.'/styles/'.$_GET['x'])) { ?>

<fieldset><legend><?php echo ucfirst($this->options["style"]); ?> css files</legend>
<?php
$dirPath = TEMPLATEPATH. '/styles/' .$this->options['style'];
if ($handle = opendir($dirPath)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_file("$dirPath/$file")) {
?>
<div class="csslist">
<?php if ($file == 'stylesheet.css') : ?>
<a href="?page=<?php echo $_GET['page']; ?>&amp;x=<?php echo $this->options["style"]; ?>&amp;y=edit&amp;file=<?php echo $file; ?>"><b><?php echo $file; ?></b></a> (Core stylesheet)
<?php else : ?>
<a href="?page=<?php echo $_GET['page']; ?>&amp;x=<?php echo $this->options["style"]; ?>&amp;y=edit&amp;file=<?php echo $file; ?>"><?php echo $file; ?></a>
<?php endif; ?>
</div>
<?php
}
}
}
closedir($handle);
}
?>
</fieldset>

<?php
if (!empty($_GET['file']) && empty($_POST['cp_save_css'])) {
$lines = file(TEMPLATEPATH.'/styles/'. $this->options['style'] .'/'. $_GET['file']);
print '<form action="" method="post">';
print '<fieldset><legend>Editing '.$_GET['file'].'</legend>';
print '<textarea class="resizable" name="css_c" id="css_c">';
foreach($lines as $line) {
print $line;
}
print '</textarea></fieldset>';
print '<input type="submit" value="Save changes" name="cp_save_css" class="button" style="color:#000" />';
print '</form>';
}
?>

<?php } ?>

<?php if ($_GET['x'] == $this->options['style'] && $_GET['y'] == 'delete' && empty($_POST['cp_delete_style'])) : ?>
<form action="" method="post">
<fieldset><legend>Deleting (<?php echo ucfirst($this->options['style']); ?>) style!</legend>
<ol><li>Click on "confirm" to proceed with deleting this style. This action cannot be undone!</li></ol>
</fieldset>
<input type="submit" value="Confirm" name="cp_delete_style" class="button" style="color:#000" />
</form>
<?php endif; ?>

</div>